package algorithms;

import test.*;

/*
 * Bubble sort;
 * sort an int array in ascend order.
 */

public class bubbleSort {
	
	/**
	 * @param args
	 */
	
	public int[] a;
	
	public bubbleSort(int[] input) {
		a = input;
	}
	
	public int[] sort() { //sort function;
		int len = a.length;
		
		for(int i=0; i<len; i++) {
			int temp=a[i];
			int p = i;
			int index=i;
			
			while((p+1) < len) {
				if(a[p+1]<temp) {
					temp = a[p+1];
					index = p+1;
				}
				
				p++;
			}
			
			a[index] = a[i];
			a[i] = temp;
		}
		
		return a;
	}
	
	public void print(int[] input) {
		System.out.print(input[0]);
		for(int i=1; i<input.length; i++) {
			System.out.print(", ");
			System.out.print(input[i]);
		}
		
		System.out.println();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int size = 10;
		inputData ind = new inputData(size);
		ind.randomIntVector();
		ind.print();
		System.out.println(" ");
		bubbleSort bs = new bubbleSort(ind.data);
		
		int b[] = bs.sort();
		System.out.println("bubble sort result: ");
		bs.print(b);
	}

}
